from flask import Blueprint, request

from utils.rate_limit_decorator import rate_limit
from utils.response import success, fail
from config import API_COOIKE, SPIDER_COOIKE

from services.analyse_service import analyse_user_service, analyse_user_service_singal_ai, analyse_fans_random

analyse = Blueprint('analyse', __name__)

# 判断这个人是否为僵尸用户
@analyse.route('/analyse_user_with_all_available_ai')
def analyse_user_with_all_available_ai():
    user_id = request.args.get('user_id', default=None, type=int)
    if not user_id:
        return fail('params is required')
    result = analyse_user_service(user_id)
    if result is None:
        return fail('weibo not found')
    return success(result)

# 单个模型判断
@analyse.route('/analyse_user_with_singal_ai', methods=['POST'])
def analyse_user_with_single_ai():
    if not request.is_json:
        return fail('request body must be json')
    params = request.get_json()
    
    if 'user_id' not in params or 'model_name' not in params or 'web_cookie' not in params or 'phone_cookie' not in params:
        return fail('params is not required')
    user_id = params['user_id']
    web_cookie = params['web_cookie']
    phone_cookie = params['phone_cookie']
    model_name = params['model_name']
    result = analyse_user_service_singal_ai(user_id, model_name, web_cookie, phone_cookie)
    if result is None:
        return success('weibo not found')
    return success(result)

@analyse.route('/analyse_user_result_list_no_cookie', methods=['POST'])
@rate_limit(interval=30)
def analyse_user_result_list_no_cookie():
    if not request.is_json:
        return fail('request body must be json')
    
    params = request.get_json()
    
    # 检查必要参数
    if 'user_id' not in params or 'model_name_list' not in params:
        return fail('params is not required')
    
    user_id = params['user_id']
    model_name_list = params['model_name_list']
    
    # 存储所有用户的分析结果
    result = []
    # 遍历用户ID列表，逐个调用单个分析接口
    for model_name in model_name_list:
        # 直接调用服务函数，避免重复的参数校验和格式化
        data = analyse_user_service_singal_ai(user_id, model_name, API_COOIKE, SPIDER_COOIKE)
        # 处理单个结果并添加到总结果列表
        if result is None:
            continue
        result.append(data)
    return success(result)

# 单个模型判断不用Cookie
@analyse.route('/analyse_user_with_singal_ai_no_cookie', methods=['POST'])
@rate_limit(interval=30)
def analyse_user_with_singal_ai_no_cookie():
    if not request.is_json:
        return fail('request body must be json')
    params = request.get_json()
    
    if 'user_id' not in params or 'model_name' not in params:
        return fail('params is not required')
    user_id = params['user_id']
    model_name = params['model_name']
    result = analyse_user_service_singal_ai(user_id, model_name, API_COOIKE, SPIDER_COOIKE)
    if result is None:
        return fail('weibo not found')
    return success(result)

# 单模型随机算法，对某个博主的20个粉丝进行计算分析
# 免Cookie
@analyse.route('/analyse_fans_no_cookie', methods=['POST'])
@rate_limit(interval=300)
def analyse_fans_no_cookie():
    if not request.is_json:
        return fail('request body must be json')
    params = request.get_json()
    
    if 'user_id' not in params or 'model_name' not in params:
        return fail('params is not required')
    user_id = params['user_id']
    model_name = params['model_name']
    result = analyse_fans_random(user_id, model_name, API_COOIKE, SPIDER_COOIKE)
    if result is None:
        return fail('weibo not found')
    if type(result) == str:
        return fail(result)
    return success(result)

@analyse.route('/analyse_fans', methods=['POST'])
def analyse_fans():
    if not request.is_json:
        return fail('request body must be json')
    params = request.get_json()
    
    if 'user_id' not in params or 'model_name' not in params or 'web_cookie' not in params or 'phone_cookie' not in params:
        return fail('params is not required')
    user_id = params['user_id']
    model_name = params['model_name']
    web_cookie = params['web_cookie']
    phone_cookie = params['phone_cookie']
    result = analyse_fans_random(user_id, model_name, web_cookie, phone_cookie)
    if result is None:
        return fail('weibo not found')
    if type(result) == str:
        return fail(result)
    return success(result)


